package com.hsy.crm.web.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.cm.Unified;
import com.hsy.crm.web.domain.entity.Order;
import com.hsy.crm.web.domain.entity.OrderAccount;
import com.hsy.crm.web.domain.entity.OrderDetail;
import com.hsy.crm.web.domain.entity.OrderResubject;
import com.hsy.crm.web.domain.entity.ProductBatch;
import com.hsy.crm.web.domain.entity.ReceiptOrderOrder;
import com.hsy.crm.web.domain.entity.StoreProduct;
import com.hsy.crm.web.domain.entity.VerificateOrderDetail;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.domain.po.order.OrderEditPo;
import com.hsy.crm.web.domain.po.order.OrderPagePo;
import com.hsy.crm.web.domain.po.order.ProductBatchPagePo;
import com.hsy.crm.web.domain.po.order.SumNumberPo;
import com.hsy.crm.web.domain.vo.order.BatchPageVo;
import com.hsy.crm.web.domain.vo.order.OrderDetailVo;
import com.hsy.crm.web.domain.vo.order.OrderGetVo;
import com.hsy.crm.web.domain.vo.order.OrderPageVo;
import com.hsy.crm.web.domain.vo.order.OrderVo;
import com.hsy.crm.web.mapper.OrderAccountMapper;
import com.hsy.crm.web.mapper.OrderDetailMapper;
import com.hsy.crm.web.mapper.OrderMapper;
import com.hsy.crm.web.mapper.OrderResubjectMapper;
import com.hsy.crm.web.mapper.ProductBatchMapper;
import com.hsy.crm.web.mapper.ReceiptOrderMapper;
import com.hsy.crm.web.mapper.VerificateOrderDetailMapper;
import com.hsy.crm.web.service.asyn.AsynOrderService;
import com.hsy.crm.web.service.base.BaseService;
import com.hsy.crm.web.service.base.CacheService;
import com.hsy.crm.web.service.base.DataInitService;
import com.hsy.crm.web.service.impl.MessageService;
import com.hsy.crm.web.service.impl.StoreProductService;
import com.hsy.crm.web.utils.DateUtils;
import com.hsy.crm.web.utils.ListUtils;
import com.hsy.crm.web.utils.LongUtils;
import com.hsy.crm.web.utils.OrderUtils;
import com.hsy.crm.web.utils.ServiceUtils;
import com.hsy.crm.web.utils.StringUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrderService extends BaseService<Order> {

    private Logger logger = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    OrderAccountMapper orderAccountMapper;

    @Autowired
    OrderResubjectMapper orderResubjectMapper;

    @Autowired
    MessageService messageService;

    @Autowired
    ReceiptOrderMapper receiptOrderMapper;

    @Autowired
    StoreProductService storeProductService;

    @Autowired
    VerificateOrderDetailMapper verificateOrderDetailMapper;

    @Autowired
    ProductBatchMapper productBatchMapper;


    @Transactional
    public Unified<Order> insert(OrderVo orderVo) {
        Order order = orderVo.getOrder();

        order = this.insert_order(order,orderVo);
        this.insert_order_details(order,orderVo);

        this.insert_order_accounts(order,orderVo);
        this.insert_order_resubjects(order,orderVo);

        if( isExamine(orderVo) ){
            examine(orderVo,App.ExamineSource.INSERT);
        }

        return Unified.newInstance(order);
    }

    @Transactional
    public Unified<Order> update(OrderVo orderVo) {
        check_order_type(orderVo.getOrder().getOrderType());

        Order order = orderVo.getOrder();
        order.setCustomerId(orderVo.getCustomerId());
        order.setCompanyId(orderVo.getLoginCompanyId());
        if( !isExamine(orderVo) ){
            orderMapper.updateByPrimaryKeySelective(order);
        }

        this.delete_order_details(order);
        this.delete_order_accounts(order);
        this.delete_order_resubjects(order);

        this.insert_order_details(order,orderVo);
        this.insert_order_accounts(order,orderVo);
        this.insert_order_resubjects(order,orderVo);

        if( isExamine(orderVo) ){
            examine(orderVo,App.ExamineSource.UPDATE);
        }

        return Unified.newInstance(order);
    }

    @Transactional
    public Unified<Order> examine(OrderVo orderVo,int examineSource) {
        Order order = orderVo.getOrder();
        logger.info("examine buy order {}",order.getId());
        check_order_type(order.getOrderType());

        if( examineSource == App.ExamineSource.INSERT ){
            order.setStatus(this.getStatus(order));
            orderMapper.insert(order);
        }else if( examineSource == App.ExamineSource.UPDATE ){
            order.setStatus(this.getStatus(order));
            orderMapper.updateByPrimaryKeySelective(order);
        }

        List<OrderDetail> orderDetails = getOrderDetails(orderVo);

        this.update_store_product(orderDetails,App.ExamType.EXAMINE,order.getOrderType(),orderVo.getCustomerId());
        this.updateDdStatus(order);
        this.addMessage(order,orderDetails,App.ExamType.EXAMINE);

        return Unified.newInstance(order);
    }

    private List<OrderDetail> getOrderDetails(OrderVo orderVo){
        List<OrderDetail> orderDetails = Lists.newArrayList();
        for( OrderDetailVo orderDetailVo : orderVo.getDetails() ){
            orderDetails.add(orderDetailVo);
        }
        return  orderDetails;
    }

    private void addMessage(Order order,List<OrderDetail> orderDetails,int examType){
        if( order.getOrderType() == App.BILL_TYPE.ORDER_CGDD || order.getOrderType() == App.BILL_TYPE.ORDER_XSDD ){
            return;
        }
        messageService.addMessage(order,orderDetails,examType);
    }

    @Transactional
    public Unified<Order> invalid(Order order) {
        logger.info("doInvalid buy order {}",order.getId());
        check_order_type(order.getOrderType());
        List<OrderDetail> orderDetails = getOrderDetails(order);

        this.order_check(order);
        this.delete_check(order);
        this.update_store_product(orderDetails,App.ExamType.INVALID,order.getOrderType(),order.getCustomerId());
        this.updateStatus(order,App.Status.BC);

        order.setCompanyId(order.getLoginCompanyId());
        this.addMessage(order,orderDetails,App.ExamType.INVALID);

        return Unified.newInstance(order);
    }

    @Transactional
    public Unified<Integer> delete(Order order) {
        logger.info("delete buy order {}",order.getId());
        this.delete_check(order);
        this.delete_order(order);
        this.delete_order_details(order);
        this.delete_order_accounts(order);
        this.delete_order_resubjects(order);
        return Unified.newInstance();
    }

    public PageResult page(OrderPageVo pageVo) {
        check_order_type(pageVo.getOrderType());
        Page page = PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        PageResult<OrderPagePo> pageResult = new PageResult<>();

        int orderType = pageVo.getOrderType();
        if( orderType == App.BILL_TYPE.ORDER_SHD || orderType == App.BILL_TYPE.ORDER_SHTHD || orderType == App.BILL_TYPE.ORDER_XSDD ){
            pageResult.setRows(orderMapper.pageMar(pageVo));
        }else if( orderType == App.BILL_TYPE.ORDER_JHD || orderType == App.BILL_TYPE.ORDER_JHTHD || orderType == App.BILL_TYPE.ORDER_CGDD ){
            pageResult.setRows(orderMapper.pagePur(pageVo));
        }

        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public PageResult batchPage(BatchPageVo pageVo) {
        Page page = PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        PageResult<ProductBatchPagePo> pageResult = new PageResult<>();

        ProductBatch productBatch = new ProductBatch();
        productBatch.setProductId(pageVo.getProductId());
        productBatch.setCustomerId(pageVo.getCustomerId());
        pageResult.setRows(productBatchMapper.page(productBatch));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public Unified<OrderEditPo> getOrder(OrderGetVo vo){
        int orderType = vo.getOrderType();
        check_order_type(orderType);

        if(LongUtils.isEmpty(vo.getId())){
            throw new ServiceException("params error");
        }

        Unified<OrderEditPo> unified = new Unified<>();
        OrderEditPo po = new OrderEditPo();
        // get order

        if( orderType == App.BILL_TYPE.ORDER_SHD || orderType == App.BILL_TYPE.ORDER_SHTHD || orderType == App.BILL_TYPE.ORDER_XSDD ){
            po.setOrder(orderMapper.getOrderMar(vo));
        }else if( orderType == App.BILL_TYPE.ORDER_JHD || orderType == App.BILL_TYPE.ORDER_JHTHD || orderType == App.BILL_TYPE.ORDER_CGDD ){
            po.setOrder(orderMapper.getOrderPur(vo));
        }

        // get order details
        OrderDetail detail = new OrderDetail();
        detail.setOrderId(vo.getId());
        po.setDetails(orderMapper.getOrderDetail(vo));

        // get order accounts
        OrderAccount account = new OrderAccount();
        account.setBillId(vo.getId());
        account.setType(App.BILL_TYPE.ORDER_CGDD);
        po.setAccounts(orderMapper.getOrderAccount(vo));

        // get order accounts
        po.setOrderResubjects(orderMapper.getOrderResubjects(vo));

        unified.setData(po);
        return unified;
    }

    private List<OrderDetail> getOrderDetails(Order order){
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setCustomerId(order.getCustomerId());
        return orderDetailMapper.select(orderDetail);
    }

    private void order_check(Order order){
        Order select = new Order();
        select.setCustomerId(order.getCustomerId());
        select.setOrderId(order.getId());
        int count = orderMapper.selectCount(select);
        if( count > 0 ){
            throw new ServiceException(App.BussError.ORDER_DELETE_ORDER.getCode());
        }
    }

    private void delete_check(Order order){
        String customerId = order.getCustomerId();
        order = orderMapper.selectByPrimaryKey(order);
        if( isBill(order.getOrderType()) ){

            //receipt
            ReceiptOrderOrder receiptOrder = new ReceiptOrderOrder();
            receiptOrder.setCustomerId(customerId);
            receiptOrder.setTargetId(order.getId());
            Long receiptId = receiptOrderMapper.selectOrder(receiptOrder);
            if( receiptId != null ){
                throw new ServiceException(App.BussError.ORDER_DELETE_RECEIPT.getCode());
            }

            // verificate
            VerificateOrderDetail verificateOrderDetail = new VerificateOrderDetail();
            verificateOrderDetail.setCustomerId(customerId);
            verificateOrderDetail.setTargetId(order.getId());
            int count = verificateOrderDetailMapper.selectCount(verificateOrderDetail);
            if( count > 0 ){
                throw new ServiceException(App.BussError.ORDER_DELETE_HX.getCode());
            }
        }
    }

    private void delete_order(Order order){
        orderMapper.deleteByPrimaryKey(order);
    }

    private void delete_order_details(Order order){
        OrderDetail detail = new OrderDetail();
        detail.setCustomerId(order.getCustomerId());
        detail.setOrderId(order.getId());
        orderDetailMapper.delete(detail);
    }

    private Order insert_order(Order order,OrderVo orderVo){

        //检查单据类型  所有单据都在一张表
        check_order_type(order.getOrderType());
        setOrderParams(order,orderVo);
        this.checkVendorExists(order);
        if( isExamine(orderVo) ){
            ServiceUtils.init_entity_insert(order);
        }else{
            this.insert(order);
        }
        return order;
    }

    private void insert_order_details(Order order,OrderVo orderVo){
        //order detail s
        List<OrderDetailVo> details = orderVo.getDetails();

        if(ListUtils.isEmpty(details)){
            throw new ServiceException("order details must > 0");
        }

        //页面判断
        //jhthCheck(order,orderVo,details);

        if( isBill(orderVo.getOrder().getOrderType())  ){
            OrderUtils.divDiscountMoney(details,orderVo.getOrder());
            OrderUtils.divPurPrice(details,orderVo.getOrder());
        }

        int orderType = orderVo.getOrder().getOrderType();

        for( OrderDetailVo detail : details ){

            check_back_details(orderVo,detail);
            check_batch_detail(orderVo,detail);

            if(!CacheService.hasProductId(orderVo.getCustomerId(),detail.getProductId())){
                throw new ServiceException(detail.getProductId());
            }

            if( detail.getPercentPrice() == null ){
                detail.setPercentPrice(0L);
            }
            if( detail.getPurPercentPrice() == null ){
                detail.setPurPercentPrice(0L);
            }
            detail.setOrderType(orderType);
            detail.setOrderId(order.getId());
            detail.setCustomerId(orderVo.getCustomerId());
            detail.setLoginUserId(orderVo.getLoginUserId());
            detail.setLoginCompanyId(orderVo.getLoginCompanyId());
            detail.setSort(DataInitService.getSort());

            ServiceUtils.init_entity_insert(detail);
            orderDetailMapper.insert(detail);
        }
    }

    /**
     * 有批次必须有保质期  有保质期可以没有批次
     * @param orderVo
     * @param detail
     */
    private void check_batch_detail(OrderVo orderVo,OrderDetailVo detail){
        if( detail.getEnableBatch() == 0 && detail.getEnableFine() == 0 ){
            return;
        }
        if( detail.getEnableBatch() == 1 ){
            if( StringUtils.isEmpty(detail.getBatchNo()) ){
                throw new ServiceException("batch no is empty");
            }
            if( LongUtils.isEmpty(detail.getProductionDate()) ){
                throw new ServiceException("product date is empty");
            }

            detail.setProductionDate(getProductDate(detail.getProductionDate()));
            if( hasBatch(orderVo,detail) ){
                return;
            }

            ProductBatch productBatch = new ProductBatch();
            productBatch.setId(UuidUtils.getUuid());
            productBatch.setCustomerId(orderVo.getCustomerId());
            productBatch.setProductId(detail.getProductId());
            productBatch.setBatchNo(detail.getBatchNo());
            productBatch.setInputTime(DateUtils.millis());
            productBatch.setProductDate(detail.getProductionDate());
            productBatchMapper.insert(productBatch);

        }
        if( detail.getEnableFine() == 1 ){
            if( LongUtils.isEmpty(detail.getProductionDate()) ){
                throw new ServiceException("product date is empty");
            }
        }
    }

    private long getProductDate(Long productDate){
        return DateUtils.parseShort(DateUtils.formatShort(new Date(productDate))).getTime();
    }

    private boolean hasBatch(OrderVo orderVo,OrderDetailVo detail){
        ProductBatch productBatch = new ProductBatch();
        productBatch.setCustomerId(orderVo.getCustomerId());
        productBatch.setProductId(detail.getProductId());
        productBatch.setBatchNo(detail.getBatchNo());
        productBatch.setProductDate(detail.getProductionDate());

        return productBatchMapper.selectCount(productBatch) > 0;
    }

    private void check_back_details(OrderVo orderVo,OrderDetailVo detail){
        int orderType = orderVo.getOrder().getOrderType();
        //退货校验  退货数量不能大于原来数量
        if( orderType != App.BILL_TYPE.ORDER_SHTHD && orderType != App.BILL_TYPE.ORDER_JHTHD ){
            return;
        }

        //指定单据退
        if(LongUtils.isNotEmpty(detail.getOrderDetailId())){
            OrderDetail pkSelect = new OrderDetail();
            pkSelect.setCustomerId(orderVo.getCustomerId());
            pkSelect.setId(detail.getOrderDetailId());
            pkSelect = orderDetailMapper.selectByPrimaryKey(pkSelect);
            if( pkSelect.getNumber().longValue() < detail.getNumber().longValue() ){
                throw new ServiceException(App.BussError.ORDER_BACK_NUMBER.getCode());
            }
        }else{
            //未指定单据退

        }
    }

    private void delete_order_accounts(Order order){
        OrderAccount account = new OrderAccount();
        account.setBillId(order.getId());
        account.setCustomerId(order.getCustomerId());
        account.setType(App.BILL_TYPE.ORDER_JHD);
        orderAccountMapper.delete(account);
    }

    private void insert_order_accounts(Order order,OrderVo orderVo){
        //account s
        List<OrderAccount> orderAccounts = orderVo.getAccounts();
        if(ListUtils.isNotEmpty(orderAccounts)){
            for (OrderAccount orderAccount : orderAccounts){
                orderAccount.setBillId(order.getId());
                orderAccount.setType(App.BILL_TYPE.ORDER_JHD);
                orderAccount.setCustomerId(orderVo.getCustomerId());

                ServiceUtils.init_entity_insert(orderAccount);
                orderAccountMapper.insert(orderAccount);
            }
        }
    }

    private int getStatus(Order dbOrder){
        //订单金额
        long dueMoney = dbOrder.getDueMoney();
        //优惠金额
        long discountMoney = dbOrder.getDiscountMoney();
        //已收金额
        long realMoney = dbOrder.getRealMoney();
        int status;
        if( realMoney == 0L ){
            status = App.StatusDD.WK;
        }else if( discountMoney + realMoney == dueMoney ){
            status = App.StatusDD.QK;
        }else if( discountMoney + realMoney > dueMoney ){
            throw new ServiceException("payment money > due money");
        }else{
            status = App.StatusDD.BFK;
        }
        return status;
    }

    private void update_store_product(List<OrderDetail> orderDetails,int actionType,int orderType,String customerId){
        if( orderType == App.BILL_TYPE.ORDER_CGDD || orderType == App.BILL_TYPE.ORDER_XSDD ){
            return;
        }

        //后续改成orderDetails 从orderService传递过来
        int direction = AsynOrderService.getDirection(actionType,orderType);
        for( OrderDetail orderDetail : orderDetails ){
            Long price = AsynOrderService.getPrice(orderDetail);
            StoreProduct storeProduct = this.getStoreProduct(customerId,orderType,orderDetail,price);
            storeProductService.update(storeProduct,direction);
            orderDetail.setStoreProductId(storeProduct.getId());
        }
    }

    private static StoreProduct getStoreProduct(String customerId,Integer orderType,OrderDetail orderDetail,Long price){
        StoreProduct storeProduct = new StoreProduct();
        storeProduct.setCompanyId(orderDetail.getCompanyId());
        storeProduct.setProductId(orderDetail.getProductId());
        storeProduct.setStoreId(orderDetail.getStoreId());
        storeProduct.setUnitId(orderDetail.getUnitId());
        storeProduct.setCustomerId(customerId);
        storeProduct.setNumber(orderDetail.getNumber());
        storeProduct.setPrice(price);
        return storeProduct;
    }

    private void updateDdStatus(Order dbOrder){
        // update DD status
        Long ddOrderId = dbOrder.getOrderId();
        if(ddOrderId != null && ddOrderId > 0){
            this.updateDdOrderStatus(dbOrder,getDdType(dbOrder.getOrderType()));
        }
    }

    private void updateStatus(Order dbOrder, int status){
        Order order = new Order();
        order.setCustomerId(dbOrder.getCustomerId());
        order.setId(dbOrder.getId());
        order.setStatus(status);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    private void updateDdOrderStatus(Order dbOrder,int ddOrderType){

        if( dbOrder.getThType() != null && dbOrder.getThType() == App.ThType.DZ ){
            return;
        }

        Long orderId = dbOrder.getOrderId();
        logger.info("updateDdOrderStatus {}",orderId);

        List<SumNumberPo> orderSumNums = orderMapper.getOrderSumNumber(dbOrder);

        if( orderId!= null && orderId > 0 ){
            Order ddOrder = new Order();
            ddOrder.setId(orderId);
            ddOrder.setCustomerId(dbOrder.getCustomerId());
            List<SumNumberPo> ddSumNums;
            if(ddOrderType == App.OrderType.CG){
                ddOrder.setOrderType(App.BILL_TYPE.ORDER_CGDD);
                ddSumNums = orderMapper.getOrderSumNumber(ddOrder);
            }else{
                ddOrder.setOrderType(App.BILL_TYPE.ORDER_XSDD);
                ddSumNums = orderMapper.getOrderSumNumber(ddOrder);
            }
            int status = getDdStatus(orderSumNums,ddSumNums);

            Order updateOrder = new Order();
            updateOrder.setStatus(status);
            updateOrder.setId(orderId);
            updateOrder.setCustomerId(dbOrder.getCustomerId());
            orderMapper.updateByPrimaryKeySelective(updateOrder);

        }
    }

    private static int getDdStatus(List<SumNumberPo> order,List<SumNumberPo> ddOrder){

        if( sumSumNumberPos(order) == 0 ){
            return App.StatusDD.WK;
        }

        for( SumNumberPo ddSumNumberPo : ddOrder ){
            SumNumberPo sumNumberPo = getSumNumberPo(order,ddSumNumberPo.getProductId());
            if( sumNumberPo == null ){
                return App.StatusDD.BFK;
            }
            if( sumNumberPo.getNumber() < ddSumNumberPo.getNumber() ){
                return App.StatusDD.BFK;
            }
        }

        return App.StatusDD.QK;
    }

    private static int sumSumNumberPos(List<SumNumberPo> order){
        int number = 0;
        if( ListUtils.isEmpty(order) ){
            return number;
        }

        for( SumNumberPo sumNumberPo : order ){
            number += sumNumberPo.getNumber();
        }
        return number;
    }

    private static SumNumberPo getSumNumberPo(List<SumNumberPo> order,long productId){
        for( SumNumberPo sumNumberPo : order ){
            if( sumNumberPo.getProductId() == productId ){
                return sumNumberPo;
            }
        }
        return null;
    }

    private void delete_order_resubjects(Order order){
        OrderResubject resubject = new OrderResubject();
        resubject.setCustomerId(order.getCustomerId());
        resubject.setOrderId(order.getId());
        orderResubjectMapper.delete(resubject);
    }

    private void insert_order_resubjects(Order order,OrderVo orderVo){
        List<OrderResubject> resubjects = orderVo.getResubjects();
        if(ListUtils.isNotEmpty(resubjects)){
            for( OrderResubject resubject : resubjects ){
                resubject.setOrderId(order.getId());
                resubject.setId(UuidUtils.getUuid());
                resubject.setCustomerId(orderVo.getCustomerId());
                ServiceUtils.init_entity_insert(resubject);
                orderResubjectMapper.insert(resubject);
            }
        }
    }

    private static boolean isBill(int orderType){
        boolean bill = false;
        switch (orderType){
            case App.BILL_TYPE.ORDER_JHD:
                bill = true;
                break;
            case App.BILL_TYPE.ORDER_JHTHD:
                bill = true;
                break;
            case App.BILL_TYPE.ORDER_SHD:
                bill = true;
                break;
            case App.BILL_TYPE.ORDER_SHTHD:
                bill = true;
                break;
            default:
                break;
        }
        return bill;
    }

    private static void check_order_type(Integer orderType){
        if( orderType == null ){
            throw new ServiceException("order type is null");
        }

        int tmp = orderType;

        if( !(tmp == App.BILL_TYPE.ORDER_JHD || tmp == App.BILL_TYPE.ORDER_SHD
                || tmp == App.BILL_TYPE.ORDER_SHTHD || tmp == App.BILL_TYPE.ORDER_JHTHD
                || tmp == App.BILL_TYPE.ORDER_CGDD || tmp == App.BILL_TYPE.ORDER_XSDD ) ){
            throw new ServiceException("order type is error");
        }
    }

    /**
     * 根据单据类型获得订单类型
     */
    private static int getDdType(int orderType){
        int detailType = -1;
        switch (orderType){
            case App.BILL_TYPE.ORDER_JHD:
                detailType = App.OrderType.CG;
                break;
            case App.BILL_TYPE.ORDER_JHTHD:
                detailType = App.OrderType.CG;
                break;
            case App.BILL_TYPE.ORDER_SHD:
                detailType = App.OrderType.XS;
                break;
            case App.BILL_TYPE.ORDER_SHTHD:
                detailType = App.OrderType.XS;
                break;
            default:
                break;
        }
        return detailType;
    }

    private boolean isExamine(OrderVo orderVo){
        return orderVo.getSaveType() == App.SaveType.EXAMINE;
    }

    // new
    private void setOrderParams(Order order,OrderVo orderVo){
        order.setCustomerId(orderVo.getCustomerId());
        order.setLoginUserId(orderVo.getLoginUserId());
        order.setLoginCompanyId(orderVo.getLoginCompanyId());
        order.setCode(DataInitService.getOrderCode());
        order.setStatus(App.Status.BC);
        order.setThStatus(App.ThStatus.NO);
        order.setVerificateMoney(0L);
        if( order.getPurPrice() == null ){
            order.setPurPrice(0L);
        }
    }

    private void checkVendorExists( Order order ){
        int orderType = order.getOrderType();
        if( orderType == App.BILL_TYPE.ORDER_JHD
                || orderType == App.BILL_TYPE.ORDER_JHTHD
                || orderType == App.BILL_TYPE.ORDER_CGDD  ){
            if( !CacheService.hasVendorId(order.getCustomerId(),order.getRelationId()) ){
                throw new ServiceException(App.BussError.VENDOR_IS_DELETE.getCode());
            }
        }else{
            if( !CacheService.hasCustomerId(order.getCustomerId(),order.getRelationId()) ){
                throw new ServiceException(App.BussError.CUSTOMER_IS_DELETE.getCode());
            }
        }
    }

}
